{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49efad96-fe01-4024-a803-ff3334536bc5",
   "metadata": {},
   "source": [
    "### Try this notebook in Google Colab, Binder or SageMaker!\n",
    "\n",
    "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/vtk/vtkImageData.ipynb)\n",
    "[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/InsightSoftwareConsortium/itkwidgets/HEAD?labpath=examples%2Fintegrations%2Fvtk%2FvtkImageData.ipynb)\n",
    "[![Open In SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github.com/InsightSoftwareConsortium/itkwidgets/blob/main/examples/integrations/vtk/vtkImageData.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "44a0e747-8e2a-46d7-8040-be711815fa6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install dependencies for this example\n",
    "import sys\n",
    "\n",
    "!{sys.executable} -m pip install -q vtk \"itkwidgets[all]>=1.0a23\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "03be0ed0-1f43-4b4a-8163-4a5730f4edda",
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib.request import urlretrieve\n",
    "import os\n",
    "\n",
    "import vtk\n",
    "\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2cf5c0bf-4a20-496b-bb8d-2f6a71f7b1bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download data\n",
    "file_name = 'vase.vti'\n",
    "if not os.path.exists(file_name):\n",
    "    url = 'https://data.kitware.com/api/v1/file/5a826bdc8d777f0685782960/download'\n",
    "    urlretrieve(url, file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "747a5e97-3216-4af1-ad62-ebc46a6dc3e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = vtk.vtkXMLImageDataReader()\n",
    "reader.SetFileName(file_name)\n",
    "reader.Update()\n",
    "vtk_image = reader.GetOutput()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "06d943d7-749d-49f0-ac5f-b1da4ebe5e27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <img id=screenshot_imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7 src=>\n",
       "                <script id=\"script_imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7\" type=\"text/javascript\">\n",
       "                    var container = document.getElementById(\"script_imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7\").parentNode;\n",
       "                    var image = document.getElementById(\"screenshot_imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7\");\n",
       "                    if (!image) {\n",
       "                        image = document.createElement(\"img\");\n",
       "                        image.id = \"screenshot_imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7\";\n",
       "                        container.appendChild(image);\n",
       "                    }\n",
       "                    image.src = \"\";\n",
       "                    var viewer = document.getElementById(\"imjoy_window_a4733fe9-56a6-439f-bab8-9bf5b13da6c7\");\n",
       "                    // Hide the static image if the Viewer is visible\n",
       "                    image.style.display = viewer ? \"none\" : \"block\";\n",
       "                </script>\n",
       "            "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "window.connectPlugin && window.connectPlugin(\"25642b8a-c4da-42db-9843-ff652cb768fa\")"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div id=\"cd4484b3-0711-425d-aa89-1f46a66e8f63\"></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<itkwidgets.viewer.Viewer at 0x7f9a22223190>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "view(vtk_image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
